﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanBuon
{
    public partial class frmTheVip : Form
    {
        public static bool kiemtra = false;
        bool checkClose = false;
        bool check = false;
        Entities.TheVip[] thevip;
        Entities.HDBanBuon temp;
        public static Entities.HDBanBuon hdbanbuon;

        public frmTheVip()
        {
            InitializeComponent();
            try
            {
                thevip = Common.StaticVariable.InterfaceObject.SelectTheVip("1");
            }
            catch
            {
            }
        }
        public frmTheVip(Entities.HDBanBuon hdbanbuon, bool kt)
        {
            InitializeComponent();
            try
            {
                thevip = Common.StaticVariable.InterfaceObject.SelectTheVip("1");
                btnThanhToan.Enabled = kt;
                this.temp = hdbanbuon;
                lblGiaTri.Text = Common.Utilities.FormatMoney(temp.GiaTriThe);
                txtMaTheVip.Text = temp.MaTheVip;
                lblMaKhachHang.Text = temp.MaKhachHang;
                lblTenKhachHang.Text = temp.TenKhachHang;
            }
            catch
            {
            }
        }

        bool LayTheVip()
        {
            try
            {
                bool kt = false;
                foreach (Entities.TheVip item in thevip)
                {
                    if (item.MaTheVip.ToUpper() == txtMaTheVip.Text.ToUpper())
                    {
                        kt = true;
                        temp.MaTheVip = item.MaTheVip;
                        temp.GiaTriThe = item.GiaTriConLai;

                        lblTenKhachHang.Text = item.TenKhachHang;
                        lblGiaTri.Text = Common.Utilities.FormatMoney(item.GiaTriConLai);
                    }
                }
                if (!kt)
                {
                    MessageBox.Show("Mã thẻ không tồn tại", Common.MSG.TieuDe, MessageBoxButtons.OK);
                }
                return kt;
            }
            catch
            {
                return false;
            }
        }

        void ThanhToan()
        {
            try
            {
                if (!btnThanhToan.Enabled)
                {
                    return;
                }
                hdbanbuon = temp;
                if (MessageBox.Show("Bạn muốn thanh toán luôn không?", Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
                else
                {
                    kiemtra = true;
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }

        void Thoat()
        {
            this.Close();
        }

        private void frmTheVip_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(frmTheVip_FormClosing);
            txtMaTheVip.KeyDown += new KeyEventHandler(txtMaTheVip_KeyDown);
            btnThanhToan.Click += new EventHandler(btnThanhToan_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
        }

        void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        void btnThanhToan_Click(object sender, EventArgs e)
        {
            ThanhToan();
        }

        void txtMaTheVip_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (LayTheVip())
                    ThanhToan();
            }
        }

        void frmTheVip_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    kiemtra = true;
                    e.Cancel = checkClose;
                }
                else
                    e.Cancel = !checkClose;
        }
    }
}
#endregion